package com.lzw.bluetoothpractice.Activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.Models._User;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.MD5Utils;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.util.Map;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
    private Map<String, String> keyMap;
    // 输入的电话号码
    private EditText telephone;
    // 第一个密码
    private EditText password;
    //昵称
    private EditText username;
    // 注册按钮
    private Button register1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
        register1.setOnClickListener(this);
    }

    private void init() {
        telephone = (EditText) this.findViewById(R.id.register1_et_telephone);
        password = (EditText) this.findViewById(R.id.register1_et_password);
        username = (EditText)this.findViewById(R.id.et_username);
        register1 = (Button) this.findViewById(R.id.bt_register1);
    }

    @Override
    public void onClick(View view) {
        // 获得电话号码
        String tele = telephone.getText().toString();
        // 获得第一个密码
        String first_pass = password.getText().toString();
        //获取昵称
        String name = username.getText().toString();
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/android/user/register");
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonParams = new JSONObject();//服务器需要传参的json对象getMD5ofStrByCharset
        jsonParams.put("phoneNumber", tele);
        jsonParams.put("passWord",first_pass);
        jsonParams.put("userName", name);
        jsonParams.put("userType",102);
        String accessToken = null;
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).accountRegister(jsonParams, "1", AppUtil.getVersionCode() + "", accessToken, keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Toast.makeText(RegisterActivity.this, "注册失败"+e.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(JSONObject json) {
                Log.e("注册",json+"");
                //ProgressDialogUtils.dismissLoadingProgress();
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                Toast.makeText(RegisterActivity.this,"注册成功，请登录",Toast.LENGTH_SHORT).show();
                startActivity(intent);
                finish();

            }
        });

    }
}
